package com.ckzp.jfinal.form;

import com.ckzp.core.util.StringUtil;
import com.ckzp.plugins.datalist.active.EActive;
import com.ckzp.plugins.datalist.active.ESelect;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 51594 on 2022/7/28.
 */
public class FormSelect {

    private EActive s = null;

    private List dataList = null;

    private String pre_sources = "";
    private String next_sources = "";
    private String sources = "";
    private String layVerify = "";

    private boolean laysearch = false;

    private List colors = null;

    public FormSelect() {
        if (s == null) s = new ESelect();
    }

    public static FormSelect n() {
        FormSelect ss = new FormSelect();
        if (ss.s == null) ss.s = new ESelect();
        return ss;
    }

    public FormSelect setName(String name) {
        s.setName(name);
        s.setId(name);
        return this;
    }

    public FormSelect setSource(String source) {
        sources = source;
        // s.setDataList(getDataList(sources));
        return this;
    }

    public FormSelect setPreSource(String pre_sources) {
        this.pre_sources = pre_sources;
        return this;
    }

    public FormSelect setNextSource(String next_sources) {
        this.next_sources = next_sources;
        return this;
    }

    public FormSelect setColor(String colors) {
        s.setShowColor(colors);
        return this;
    }


    public FormSelect setDefaultString(String str) {
        s.setCanNull(true);
        s.setDefaultString(str);
        return this;
    }

    public FormSelect setCanNull(boolean str) {
        s.setCanNull(str);
        return this;
    }


    public FormSelect setReadOnly(boolean str) {
        s.setReadOnly(str);
        return this;
    }


    public FormSelect setEvent(String str) {
        s.setEvent(str);
        return this;
    }

    public FormSelect setStyle(String str) {
        s.setStyle(str);
        return this;
    }

    public FormSelect setLayVerify(String verify) {
        s.addEvent(" lay-verify=\"" + verify + "\" ");
        return this;
    }


    public FormSelect setLayFilter(String filter) {
        s.addEvent(" lay-filter=\"" + filter + "\" ");
        return this;
    }

    public FormSelect setLaySearch(boolean search) {
        if (search) s.addEvent(" lay-search");
        return this;
    }


    public String showHtml() {
        s.setDataList(getDataList(sources));
        return s.showHtml();
    }

    public String showHtml(String values) {
        s.setDataList(getDataList(sources));
        return s.showHtml(values);
    }

    public String showText(String values) {
        s.setDataList(getDataList(sources));
        return s.showNoColorHtml(values);
    }

    public String showHtml(int values) {
        s.setDataList(getDataList(sources));
        return s.showHtml(values);
    }

    public String showText(int values) {
        s.setDataList(getDataList(sources));
        return s.showNoColorHtml(String.valueOf(values));
    }


    public String showColor(int values) {
        return s.showColor(String.valueOf(values));
    }


    public String showColor(String values) {
        return s.showColor(String.valueOf(values));
    }

    public List getDataList(String source) {

        if (dataList != null && dataList.size() > 0) return dataList;

        StringBuffer sb = new StringBuffer();
        if (pre_sources != null && pre_sources.length() > 0) sb.append(pre_sources);
        if (source.indexOf("select") > -1 || source.indexOf("SELECT") > -1) {
            try {
                List list = Db.find(source);
                for (int k = 0; k < list.size(); k++) {
                    Record r = (Record) list.get(k);
                    // dataList.add(new String[]{r.getStr("id"), r.getStr("name")});
                    sb.append(r.getStr("id") + "`" + r.getStr("name") + "`");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (source.indexOf("`") > -1) {
            sb.append(sources);
        }
        if (next_sources != null && next_sources.length() > 0) sb.append(next_sources);

        if (dataList == null) dataList = new ArrayList();
        List ll = StringUtil.splitList(sb.toString(), "`");
        if (ll != null) {
            dataList.clear();
            if (ll.size() > 1) {
                for (int k = 0; k < ll.size(); k += 2) {
                    String s = String.valueOf(ll.get(k));
                    String s1 = (k >= ll.size() - 1) ? "" : String.valueOf(ll.get(k + 1));
                    dataList.add(new String[]{s, s1});
                }
            }
        }

        if (dataList.size() > 0) {
            return dataList;
        }

        if (dataList.size() == 0) {
            dataList.add(new String[]{"", "数据为空"});
            return dataList;
        }
        return dataList;
    }

    /**
     * @return
     */
    public String getFirstValue() {
        if (dataList != null && dataList.size() > 0) {
            return ((String[]) dataList.get(0))[0];
        }
        return "";
    }
}